﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;
using TakePhotoTogether.Utils;
using Microsoft.Devices;

namespace TakePhotoTogether.View
{
    public partial class FTU : UserControl
    {
        private MainPage m_mainPage;
        private GalleryPage m_galleryPage;
        private const string DEBUG_TAG = "TakePhotoTogether.View.FTU => ";

        public FtuFile.State CurrentState { get; set; }

        public FTU()
        {
            InitializeComponent();

            CurrentState = Utils.FtuFile.getState("state");

            // update state
            if (CurrentState == FtuFile.State.Closed)
            {
                Utils.FtuFile.setState(FtuFile.State.Welcome);
                CurrentState = FtuFile.State.Welcome;
                MainPage.PhotoSecurityLock = true;
            }
           
        }

        private void CameraButtons_ShutterKeyReleased(object sender, EventArgs e)
        {
            
        }

        internal void SetParent(MainPage mainPage)
        {
            m_mainPage = mainPage;
        }

        internal void SetParent(GalleryPage galleryPage)
        {
            m_galleryPage = galleryPage;
        }

        public void UpdateState(String appState)
        {
            bool showFTU = false;
            String state_str = null;      

            switch (CurrentState)
            {
                case FtuFile.State.Welcome:
                    if (CurrentState == FtuFile.State.Welcome && (appState.Equals("PreTap") || appState.Equals("MyView") || appState.Equals("NoTap")))
                    {
                        CurrentState = FtuFile.State.MyView;
                        state_str = "MyView";
                        showFTU = true;
                    }
                    break;

                // MyView = 1
                case FtuFile.State.MyView:
                    // MV_Connected = 2
                    // MV_Gallery = 3

                    showFTU = true;
                    if (appState.Equals("Connected"))
                    {
                        CurrentState = FtuFile.State.MV_Connected;
                        state_str = "Connected";
                    }
                    else if (appState.Equals("Gallery"))
                    {
                        CurrentState = FtuFile.State.MV_Gallery;
                        state_str = "Gallery";
                    }
                    else
                    {
                        showFTU = false;
                    }

                    break;

                // MV_Connected = 2
                case FtuFile.State.MV_Connected:
                    // MV_C_SplitView = 4
                    // MV_C_Gallery = 5

                    showFTU = true;
                    if (appState.Equals("SplitView"))
                    {
                        CurrentState = FtuFile.State.MV_C_SplitView;
                        state_str = "SplitView";
                    }
                    else if (appState.Equals("Gallery"))
                    {
                        CurrentState = FtuFile.State.MV_C_Gallery;
                        state_str = "Gallery";
                    }
                    else
                    {
                        showFTU = false;
                    }
                    break;

                // MV_Gallery = 3
                case FtuFile.State.MV_Gallery:
                    // MV_G_Connected = 6

                    showFTU = true;
                    if (appState.Equals("Connecting"))
                    {
                        CurrentState = FtuFile.State.MV_G_Connected;
                        state_str = "Connected";
                    }
                    if (appState.Equals("PreTap"))
                    {
                        this.Visibility = Visibility.Collapsed;
                        showFTU = false;
                    }
                    else
                    {
                        showFTU = false;
                    }

                    break;

                // MV_C_SplitView = 4
                case FtuFile.State.MV_C_SplitView:
                    // MV_C_SV_Gallery = 7

                    showFTU = true;
                    if (appState.Equals("Gallery"))
                    {
                        CurrentState = FtuFile.State.MV_C_SV_Gallery;
                        state_str = "Gallery";
                    }
                    else
                    {
                        showFTU = false;
                    }
                    break;

                // MV_C_Gallery = 5
                case FtuFile.State.MV_C_Gallery:
                    // MV_C_G_SplitView = 8

                    showFTU = true;
                    if (appState.Equals("SplitView"))
                    {
                        CurrentState = FtuFile.State.MV_C_G_SplitView;
                        state_str = "SplitView";
                    }
                    else
                    {
                        showFTU = false;
                    }
                    break;

                // MV_G_Connected = 6
                case FtuFile.State.MV_G_Connected:
                    // MV_G_C_SplitView = 9

                    showFTU = true;
                    if (appState.Equals("SplitView"))
                    {
                        CurrentState = FtuFile.State.MV_G_C_SplitView;
                        state_str = "SplitView";
                    }
                    else
                    {
                        showFTU = false;
                    }
                    break;

                default:
                    break;
            }

            if (showFTU)
            {
                this.Visibility = Visibility.Visible;
                if (state_str != null)
                VisualStateManager.GoToState(this, state_str, true);
            }
        }

        private void buttonMyView_Click(object sender, RoutedEventArgs e)
        {
            MainPage.PhotoSecurityLock = false;
            this.Visibility = Visibility.Collapsed;
            CurrentState = FtuFile.State.MyView;
            Utils.FtuFile.setState(FtuFile.State.MyView);
        }

        private void buttonSplitView_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;

            if (CurrentState == FtuFile.State.MV_Connected)
            {
                CurrentState = FtuFile.State.MV_C_SplitView;
            }
            else if (CurrentState == FtuFile.State.MV_C_Gallery)
            {
                CurrentState = FtuFile.State.MV_C_G_SplitView;
            }
            else if (CurrentState == FtuFile.State.MV_G_Connected)
            {
                CurrentState = FtuFile.State.MV_G_C_SplitView;
            }

            Utils.FtuFile.setState(CurrentState);
        }

        private void buttonGallery_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;

            if (CurrentState == FtuFile.State.MyView)
            {
                CurrentState = FtuFile.State.MV_Gallery;
            }
            else if (CurrentState == FtuFile.State.MV_Connected)
            {
                CurrentState = FtuFile.State.MV_C_Gallery;
            }
            else if (CurrentState == FtuFile.State.MV_C_SplitView)
            {
                CurrentState = FtuFile.State.MV_C_SV_Gallery;
            }

            Utils.FtuFile.setState(CurrentState);
        }

        private void buttonConnected_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;

            if (CurrentState == FtuFile.State.MyView)
            {
                CurrentState = FtuFile.State.MV_Connected;
            }
            else if (CurrentState == FtuFile.State.MV_Gallery)
            {
                CurrentState = FtuFile.State.MV_G_Connected;
            }

            Utils.FtuFile.setState(CurrentState);
        }

        private void onConnectedCompleted(object sender, EventArgs e)
        {
            VisualStateManager.GoToState(this, "ConnectedLoop", true);
        }

    }
}
